package com.jason.apps.dashboard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.apps.realtyportal.R;
import com.forimageandlist.ImageLoader;
import com.jason.apps.dashboard.AddProperty.AddOnClickSelectedListener;
import com.jason.apps.dashboard.EditProperty.EditOnClickSelectedListener;
import com.quickaction.ActionItemText;
import com.quickaction.QuickActionView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class EditPropertyAdapter extends BaseAdapter{


	private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    HashMap<String, String> property = new HashMap<String, String>();
    EditOnClickSelectedListener mCallback;
    AddOnClickSelectedListener mcallback2;
    
    public EditPropertyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        mCallback = (EditOnClickSelectedListener) activity;
        mcallback2 = (AddOnClickSelectedListener) activity;
     
     
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }


    @SuppressLint("ParserError")
	public View getView(final int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView name = (TextView)vi.findViewById(R.id.title); 
        TextView type = (TextView)vi.findViewById(R.id.artist); 
        TextView address = (TextView)vi.findViewById(R.id.contact); 
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); 
        
        
        
        property=data.get(position);
        
        name.setText(property.get("property_name"));
        type.setText(property.get("type"));
        address.setText(property.get("price"));
        
        imageLoader.DisplayImage(property.get("image"), thumb_image);
        
        vi.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				
				//mCallback.OnClickSeletedListener(v,position);
				
				//Toast.makeText(activity, "Click here", Toast.LENGTH_LONG).show();
				QuickActionView qa = QuickActionView.Builder( v );
				
				// set the adapter
				qa.setAdapter( new CustomAdapter( activity ) );
				
				// set the number of columns ( setting -1 for auto )
				//qa.setNumColumns( (int) (2 + (Math.random() * 10)) );
				qa.setOnClickListener( new DialogInterface.OnClickListener() {

					
					public void onClick( DialogInterface dialog, int which ) {
						dialog.dismiss();
						//Toast.makeText( activity, "Selected item: " + which, Toast.LENGTH_SHORT ).show();
						if(which==0){
							mCallback.onArticleSelected(v,position);
						}
						else if(which==1){
							mCallback.ShowDetailOnArticleSelected(v, position);
						}
						else{
							mcallback2.AddArticleSelected(v, position);
						}
						
					}
				} );
				
				// finally show the view
				qa.show();
			}
		});
        return vi;

}

static class CustomAdapter extends BaseAdapter {
	
	static final int[] ICONS = new int[]{
		R.drawable.edit,
		R.drawable.view_detail,
		R.drawable.add
	};
	String[] name=new String[]{
			"Edit","View","Add"
	};

	LayoutInflater mLayoutInflater;
	List<ActionItemText> mItems;

	public CustomAdapter( Context context ) {
		mLayoutInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

		mItems = new ArrayList<ActionItemText>();
		//int total = (int) (4 + (Math.random() * 30));

        for(int i=0;i<3;i++){
		ActionItemText item = new ActionItemText( context, name[i], ICONS[i] );
	    mItems.add( item );
        }
	}
	


	public int getCount() {
		return mItems.size();
	}


	public Object getItem( int arg0 ) {
		return mItems.get( arg0 );
	}


	public long getItemId( int arg0 ) {
		return arg0;
	}

	public View getView( int position, View arg1, ViewGroup arg2 ) {
		
		View view = mLayoutInflater.inflate( R.layout.action_item, arg2, false );

		ActionItemText item = (ActionItemText) getItem( position );

		ImageView image = (ImageView) view.findViewById( R.id.image);
		TextView text = (TextView) view.findViewById( R.id.title );

		image.setImageDrawable( item.getIcon() );
		text.setText( item.getTitle() );
		text.setTextColor(Color.WHITE);

		return view;
	}

};
}
